// Project: vacuum sensor
// FileName: vacuum sensor.c


#include "./USB/usb.h"
#include "./USB/usb_function_cdc.h"
#include "HardwareProfile.h"
#include "Compiler.h"
#include "usb_config.h"
#include "USB\usb_device.h"
#include "USB\usb.h"
#include <delays.h>
#include "lib_adc.h"
#include <timers.h>

/** CONFIGURATION **************************************************/

#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config FOSC = HS
#pragma config PLLEN = ON
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config BORV = 30
#pragma config WDTEN = OFF
#pragma config WDTPS = 32768
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config BBSIZ = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTRB = OFF

/** V A R I A B L E S ********************************************************/
#pragma udata

char input_buffer[64];
char buf[64];
unsigned int n,N;
unsigned char i,j,jj,q;
double NN;

#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)

/** P R I V A T E P R O T O T Y P E S ***************************************/
static void InitializeSystem(void);
void ProcessIO(void);
void USBDeviceTasks(void);
void YourHighPriorityISRCode();
void YourLowPriorityISRCode();
void UserInit(void);

/** VECTOR REMAPPING ***********************************************/

#define REMAPPED_RESET_VECTOR_ADDRESS 0x00
#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x08
#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x18

#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
void Remapped_High_ISR (void){
_asm goto YourHighPriorityISRCode _endasm
}
#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
void Remapped_Low_ISR (void){
_asm goto YourLowPriorityISRCode _endasm
}
#pragma code

#pragma interrupt YourHighPriorityISRCode
void YourHighPriorityISRCode(){
USBDeviceTasks(); //USB_POLLING
}
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode(){
}
#pragma code




void main(void){

InitializeSystem();

PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b10000011; //VREF+,VREF-,AN9 入力用
ANSEL = 0b00000000; //ADコンバーター AN9使用
ANSELH = 0b00000010;


while(1){

if(USB_BUS_SENSE && (USBGetDeviceState() == DETACHED_STATE)){
USBDeviceAttach();
}
USBDeviceTasks();


ProcessIO();
}
}



static void InitializeSystem(void){

UserInit();
USBDeviceInit();
}


void UserInit(void){

mInitAllLEDs();
mInitAllSwitches();

n = 0;
}




void ProcessIO(void){

if(getsUSBUSART(input_buffer,32)){
switch(input_buffer[0]){ // 最初の1文字チェック

case '0':

for(j=0;j<2;j++){
if(USBUSARTIsTxTrfReady()) // 送信可能か?
putrsUSBUSART("OK");
CDCTxService();
}
break;


case '1':

for(i=0; i<63; i++) buf[i] = '0';
N=0; NN=0;

OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VREFPLUS &
ADC_REF_VDD_VREFMINUS,
ADC_CH9 &
ADC_INT_OFF );

ConvertADC();
while(BusyADC());
n=(ADRESH*256) + ADRESL;
//if(n > 1000){ // max 1000
//n = 1000;
//};
N = N + n;

NN = N; // unsigned int N を doubleNN に型変換
NN = NN * 0.9766; // 補正計算
N = NN; // doubleNN を unsigned int N に型変換

Delay_100mS;


buf[4] = (n % 10) + '0'; n = n / 10;//下4桁を取る
buf[3] = (n % 10) + '0'; n = n / 10;
buf[2] = (n % 10) + '0'; n = n / 10;
buf[1] = (n % 10) + '0';

buf[5] = '\n';

for(jj=0;jj<2;jj++){
if(USBUSARTIsTxTrfReady())
putUSBUSART(buf, 12);
CDCTxService();
}
break;

default:
break;
}
}
}



//* ボーレート変更関数  USB側とUSARTの通信速度が同じでなければならない/

#if defined(USB_CDC_SET_LINE_CODING_HANDLER)
void mySetLineCodingHandler(void){

//If the request is not in a valid range
if(cdc_notice.GetLineCoding.dwDTERate.Val > 115200)
{ // 115kbps以上なら何もしない
}
else
{
DWORD_VAL dwBaud;

// CDCドライバのボーレート変更
CDCSetBaudRate(cdc_notice.GetLineCoding.dwDTERate.Val);
// USARTのボーレート変更
dwBaud.Val = (DWORD)(GetSystemClock()/4)/line_coding.dwDTERate.Val-1;
SPBRG = dwBaud.v[0];
SPBRGH = dwBaud.v[1];
}
}

#endif






// (ここから下は変更無し)


void USBCBSuspend(void)
{
#if defined(__C30__)
#if 0
U1EIR = 0xFFFF;
U1IR = 0xFFFF;
U1OTGIR = 0xFFFF;
IFS5bits.USB1IF = 0;
IEC5bits.USB1IE = 1;
U1OTGIEbits.ACTVIE = 1;
U1OTGIRbits.ACTVIF = 1;
Sleep();
#endif
#endif
}

#if 0
void __attribute__ ((interrupt)) _USB1Interrupt(void)
{
#if !defined(self_powered)
if(U1OTGIRbits.ACTVIF) {
IEC5bits.USB1IE = 0;
U1OTGIEbits.ACTVIE = 0;
IFS5bits.USB1IF = 0;

//USBClearInterruptFlag(USBActivityIFReg,USBActivityIFBitNum);
USBClearInterruptFlag(USBIdleIFReg,USBIdleIFBitNum);
//USBSuspendControl = 0;
}
#endif
}
#endif

void USBCBWakeFromSuspend(void)
{

}

void USBCB_SOF_Handler(void)
{

}

void USBCBErrorHandler(void)
{

}

void USBCBCheckOtherReq(void)
{
USBCheckCDCRequest();
}

void USBCBStdSetDscHandler(void)
{

}

void USBCBInitEP(void)
{
CDCInitEP();
}

void USBCBSendResume(void)
{
static WORD delay_count;

USBResumeControl = 1; // Start RESUME signaling

delay_count = 1800U; // Set RESUME line for 1-13 ms
do {
delay_count--;
} while(delay_count);
USBResumeControl = 0;
}

#if defined(ENABLE_EP0_DATA_RECEIVED_CALLBACK)
void USBCBEP0DataReceived(void)
{

}
#endif

BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, WORD size)
{
switch(event) {
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_BUS_ERROR:
USBCBErrorHandler();
break;
case EVENT_TRANSFER:
Nop();
break;
default:
break;
}
return TRUE;
}








                  戻る